package main.Web;

import com.alibaba.fastjson.JSON;
import main.Service.ChatMessageService;
import main.ServiceImpli.ChatMessageServiceImpl;
import main.pojo.ChatMessage;
import main.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/26 19:45
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
@WebServlet("/UploadString")
public class UploadString extends HttpServlet {
    ChatMessageService chatMessageService=new ChatMessageServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Map<String,Object>map = new HashMap<>();
        String message=req.getParameter("message");
        System.out.println(message);
        ChatMessage chatMessage=new ChatMessage();
        chatMessage.setSenderID(((User)req.getSession().getAttribute("user")).getUserID());
        chatMessage.setReceiverID(((User)req.getSession().getAttribute("Friend")).getUserID());
        chatMessage.setChatRoomID(8);
        chatMessage.setMessageType("Text");
        chatMessage.setMessageContent(message);
        chatMessage.setSendTime(new java.util.Date());
        Boolean result=chatMessageService.createChatMessage(chatMessage);
        System.out.println(result);
        String json = JSON.toJSONString(map);
        PrintWriter pw = resp.getWriter();
        pw.print(json);
    }
}